package com.gsi.webstore.platform.storefront.pipelet.orderprocess;

import com.intershop.beehive.bts.capi.orderprocess.ProductLineItem;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.component.shipping_data.capi.shippingmethod.ShippingMethod;
import java.util.Iterator;

/**
 * 
 */
public class UpdateLineItemShippingMethodSelection extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key
     * 'ProductLineItem'
     */
    public static final String DN_PRODUCT_LINE_ITEM = "ProductLineItem";
   
    /**
     * Constant used to access the pipeline dictionary with key 'ShippingMethod'
     */
    public static final String DN_SHIPPING_METHOD = "ShippingMethod";
    
    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict) throws PipeletExecutionException
    {        
        // lookup 'ProductLineItem' in pipeline dictionary
        ProductLineItem productLineItem = (ProductLineItem)dict.get(DN_PRODUCT_LINE_ITEM);
        if (null == productLineItem)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'ProductLineItem' not available in pipeline dictionary.");
        
        }

        // lookup 'ShippingMethod' in pipeline dictionary
        ShippingMethod shippingMethod = (ShippingMethod)dict.get(DN_SHIPPING_METHOD);
        if (null == shippingMethod)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'ShippingMethod' not available in pipeline dictionary.");

        }
         
        productLineItem.setShippingMethod(shippingMethod);
        return PIPELET_NEXT;
    }
}